package com.wenwu.controller.work;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.wenwu.common.bean.RelicsWork;
import com.wenwu.common.page.PageUtils;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.common.utils.JsonUtils;
import com.wenwu.service.WorkMainService;

/**
 * 
 * @author Yaphet
 *
 */
@Controller(value = "WorkMainController")
@RequestMapping(value = "/mgr/workmain")
public class WorkMainController {

	Logger log = Logger.getLogger(this.getClass());
	
	private JsonGenerator jsonGenerator;

	//@Autowired
	//private CommonService commonService;
	@Autowired
	private WorkMainService workMainService;
	
	/**
	 * 进入主列表页面
	 * @return
	 */
	@RequestMapping(value = "/init")
	public String init(){
		return "wmain/init";
	}
	
	/**
	 * 可移动文物登记表表单查询
	 * @return
	 */
	@RequestMapping(value = "/showList")
	public void showList(RelicsWork relicsWork,Model model, HttpServletRequest request,
			HttpServletResponse response) {
		PaginationVO pageVo = PageUtils.getPaginationInfo(request);
		try {
			if(relicsWork!=null && relicsWork.getCode() != null){
				relicsWork.setCode(relicsWork.getCode().trim());
			}
			if(relicsWork!=null && relicsWork.getCode() != null){
				relicsWork.setProjectName(relicsWork.getProjectName().trim());
			}
			pageVo = workMainService.selectByCondition(relicsWork, pageVo);
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(pageVo);// 将PaginationVO对象以json格式写回页面
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage(),e);
			model.addAttribute("errorMsg", "系统异常，请稍后重试!");
		}
	}
	
	/**
	 * 添加
	 */
	@RequestMapping(value = "/saveaddForm")
	public void addBasic(RelicsWork relicsWork,HttpServletResponse response) throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		log.info("添加/修改数据：");
		try {
			jsonGenerator.writeObject(workMainService.addObj(relicsWork));
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage(),e);
			jsonGenerator.writeObject(e.getMessage());
		}
	}
	
	/**
	 * 删除信息
	 * 
	 * @param response
	 * @param request
	 * @param key
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteInfo")
	public void deleteInfo(HttpServletResponse response,
			HttpServletRequest request,
			@RequestParam(required = false, value = "keys") String keys)
			throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		try {
			String result = workMainService.deleteBykeys(keys);
			jsonGenerator.writeObject(result);
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			jsonGenerator.writeObject("删除数据失败");
		}
	}
	
	/**
	 * 进入新增表单页面
	 * @return
	 */
	@RequestMapping(value = "/addForm")
	public String dictionaryform() {
		log.info("进入基本信息表单页面...");
		/*
		try {
			if (workId != null) {
				List<RelicsAttachment> attachments;
				attachments = commonService.selectAttachmentListByKey(
							workId, AttachmentFrom.RELICS_ENTITY_01);
				model.addAttribute("attachments", attachments);
			}
		} catch (RelicsException e) {
			log.info(e.getMessage(),e);
		}*/
		return "wmain/wmainform";
	}
	
	/**
	 * 显示要修改的信息
	 * 
	 * @param response
	 * @param userId
	 * @throws Exception
	 */
	@RequestMapping(value = "/showUpdate")
	public void showUpdate(HttpServletResponse response,
			@RequestParam(value = "workId") Integer workId){
		if (workId != null) {
			try{
				RelicsWork relicsWork = workMainService.selectBySelftKey(workId);
				jsonGenerator = JsonUtils.getJsonGenerator(response);
				jsonGenerator.writeObject(relicsWork);
			}catch (Exception e){
				log.info(e.getMessage(),e);
			}
		}
	}
}
